package com.healthcloud.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.healthcloud.dao.DictMapper;
import com.healthcloud.entity.BaseDictListDate;
import com.healthcloud.entity.Dict;
import com.healthcloud.service.DictService;

/**
 * Created by xiam on 2015/6/8.
 */
@Service
@Transactional(readOnly = true)
public class DictServiceImpl implements DictService {

	@Autowired
	private DictMapper dictMapper;

	@Override
	public List<Dict> findAllByType(String type) {
		return dictMapper.findAllByType(type);
	}

	@Override
	public List<Dict> findByType(String type) {
		return dictMapper.findByType(type);
	}

	@Override
	public Map<Integer, String> findDictMap(String type) {
		List<Dict> list = dictMapper.findAllByType(type);
		Map<Integer, String> map = new HashMap<>();
		for (Dict dict : list) {
			map.put(dict.getCode(), dict.getName());
		}
		return map;
	}

	@Override
	@Transactional
	public Dict save(Dict dict) {
		dictMapper.save(dict);
		return dict;
	}

	@Override
	@Transactional
	public int logicDelete(Long id) {
		Dict dict = dictMapper.get(id);
		if (dict != null) {
			dict.setIsDel(Boolean.TRUE);
			dict.setUpdatedTime(new Date());
			return dictMapper.update(dict);
		}
		return 0;
	}

	@Override
	@Transactional
	public void update(Dict dict) {
		dict.setUpdatedTime(new Date());
		dictMapper.update(dict);
	}

	@Override
	public String getCodeName(String type, Integer code) {
		return null;
	}

	@Override
	public ArrayList<BaseDictListDate> getDictListWithType(String type) {
		List<Dict> list = dictMapper.findAllByType(type);
		ArrayList<BaseDictListDate> baseDictListDates = new ArrayList<>();
		for (Dict dict : list) {
			BaseDictListDate baseDictListDate = new BaseDictListDate();
			baseDictListDate.setCode(dict.getCode());
			baseDictListDate.setName(dict.getName());
			baseDictListDates.add(baseDictListDate);
		}
		return baseDictListDates;
	}
}
